package com.leyou.item.web;

import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @package com.leyou.item.web
 * @description: 商品规格组controller层
 * @author: 许超越
 * @date 2019/6/26 19:18
 * @version: V1.0
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 查询商品规格组
     *
     * @return
     * @param: cid 商品分类id
     */
    @GetMapping("groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCid(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(specService.querySpecGroupByCid(cid));
    }

    /**
     * 查询品牌规格参数
     *
     * @return
     * @param: gid 品牌规格分组的组id
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "searching",required = false) Boolean searching
    ) {
        return ResponseEntity.ok(specService.querySpecParams(gid,cid,searching));
    }

    /**
     * 新增商品规格分组
     *
     * @return
     * @param: specGroup 商品规格分组的信息封装对象
     */
    @PostMapping("group")
    public ResponseEntity<Void> insertSpecGroup(@RequestBody SpecGroup specGroup) {
        specService.insertSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 更新商品规格分组
     *
     * @return
     * @param: specGroup 品牌规格分组信息封装对象
     */
    @PutMapping("group")
    public ResponseEntity<Void> updateSpecGroup(@RequestBody SpecGroup specGroup) {
        specService.updateSpecGroup(specGroup);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据规格分组id删除规格分组
     *
     * @return
     * @param: id 规格分组id
     */
    @DeleteMapping("group/{id}")
    public ResponseEntity<Void> deleteSpecGroup(@PathVariable("id") Long id) {
        specService.deleteSpecGroup(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 新增规格参数
     *
     * @return
     * @param: specParam 规格参数信息
     */
    @PostMapping("param")
    public ResponseEntity<Void> insertSpecParam(@RequestBody SpecParam specParam) {
        specService.insertSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 修改规格参数
     *
     * @param: specParam 规格参数信息
     */
    @PutMapping("param")
    public ResponseEntity<Void> updateSpecParam(@RequestBody SpecParam specParam) {
        specService.updateSpecParam(specParam);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据规格参数id删除规格参数
     * @param: id 规格参数信息
     * @return
     */
    @DeleteMapping("param/{id}")
    public ResponseEntity<Void> deleteSpecParam(@PathVariable("id") Long id) {
        specService.deleteSpecParam(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 查询规格参数组，及组内参数
     * @param id 商品分类id
     * @return 规格组及组内参数
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> querySpecsByCid(@RequestParam("id") Long id) {
        return ResponseEntity.ok(specService.querySpecsByCid(id));
    }
}
